package edu.xhu.adapters;

import java.util.ArrayList;

import com.bean.LibraryBookBean;

import edu.xhu.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 图书馆中书籍listview的适配器，搜索图书和我借的图书中共同使用
 * @author swerit
 *
 */

public class LibraryAdapter extends BaseAdapter{

	private ArrayList<LibraryBookBean> mLibraryData;
	private LayoutInflater inflater;
	
	public LibraryAdapter(Context context, ArrayList<LibraryBookBean> mLibraryData){
		this.mLibraryData = mLibraryData;
		inflater = LayoutInflater.from(context);
	}
	
	
	public int getCount() {
		if (mLibraryData == null)
			return 0;
		return mLibraryData.size();
	}
	
	public void addItems(ArrayList<LibraryBookBean> mLibraryData){
		this.mLibraryData.addAll(mLibraryData);
		notifyDataSetChanged();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null){
			viewHolder		= new ViewHolder();
			convertView		= inflater.inflate(R.layout.book_item, null);
			viewHolder.name = (TextView) convertView.findViewById(R.id.textview_bookname);
			viewHolder.place = (TextView) convertView.findViewById(R.id.textview_place);
			viewHolder.language = (TextView) convertView.findViewById(R.id.textview_language);
			viewHolder.author = (TextView) convertView.findViewById(R.id.textview_author);
			viewHolder.totalCount = (TextView) convertView.findViewById(R.id.textview_total_count);
			viewHolder.publishtime = (TextView) convertView.findViewById(R.id.textview_publish_time);
			viewHolder.availableCount = (TextView) convertView.findViewById(R.id.textview_available);
			
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		LibraryBookBean bookBean = mLibraryData.get(position);
		viewHolder.name.setText(bookBean.title);
		viewHolder.place.setText(bookBean.location);
		viewHolder.language.setText(bookBean.version);
		viewHolder.author.setText(bookBean.author);
		viewHolder.totalCount.setText(bookBean.book_total);
		viewHolder.availableCount.setText(bookBean.book_avalible);
		
//		if (getCount() == 1){
//			convertView.setBackgroundResource(R.drawable.full_selector);
//		}else {
//			if (0 == position){
//				convertView.setBackgroundResource(R.drawable.top_selector);
//			}else if (getCount()-1 == position){
//				convertView.setBackgroundResource(R.drawable.bottom_selector);
//			}else {
//				convertView.setBackgroundResource(R.drawable.middle_selector);
//			}
//		}
		
		return convertView;
	}
	
	class ViewHolder{
		public TextView name;
		public TextView place;
		public TextView language;
		public TextView author;
		public TextView totalCount;
		public TextView publisher;
		public TextView publishtime;
		public TextView availableCount;
	}
}
